package com.xxx.video.commen.utils;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtils {

    private static FileUtils mFileUtils = null;

    private FileUtils() {
    }

    public static FileUtils getInstance() {
        if (mFileUtils == null) {
            synchronized (FileUtils.class) {
                if (mFileUtils == null) {
                    mFileUtils = new FileUtils();
                }
            }
        }
        return mFileUtils;
    }

    /**
     * check file is exists
     *
     * @param path file path
     * @return is it exists
     */
    public boolean fileIsExist(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        try {
            File file = new File(path);
            return file.exists();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * is it a directory
     *
     * @param path file path
     * @return check it is a directory
     */
    public boolean fileIsDirectory(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        try {
            File file = new File(path);
            return file.isDirectory();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * get application cache dir
     *
     * @param context context
     * @return cache file
     */
    public File getExternalCacheDir(Context context) {
        File cacheDir = context.getExternalCacheDir();
        if (cacheDir == null) {
            cacheDir = context.getCacheDir();
        }
        return cacheDir;
    }

    /**
     * check file path, if path isn't exists ,it will create it
     *
     * @param localFilePath file path
     */
    public void checkLocalFilePath(String localFilePath) {
        try {
            if (TextUtils.isEmpty(localFilePath)) {
                throw new Exception("file path can't empty");
            }
            File path = new File(localFilePath.substring(0,
                    localFilePath.lastIndexOf("/") + 1));
            File file = new File(localFilePath);
            if (!path.exists()) {
                path.mkdirs();
            }
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * create ImageFile
     *
     * @param context context
     * @return file
     * @throws IOException
     */
    public File createImageFile(Context context) throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        /*
         * prefix
         * suffix
         * directory
         */
        File image = File.createTempFile(imageFileName, ".jpg", storageDir);
        // Save a file: path for use with ACTION_VIEW intents
        return image;
    }

}
